<template>
  <div>
    <span>全选:</span>
    <input v-model="isAll" type="checkbox"/>
    <button @click="btnFn">反选</button>
    <ul>
      <li v-for="(item, index) in arr" :key="index">
        <input v-model="item.c" type="checkbox"/>
        <span>{{item.name}}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      arr: [
        {
          name: "猪八戒",
          c: false,
        },
        {
          name: "孙悟空",
          c: false,
        },
        {
          name: "唐僧",
          c: false,
        },
        {
          name: "白龙马",
          c: false,
        },
      ],
    };
  },
  computed: {
    isAll:{
      // 接收的val是赋给isAll(双绑)的值
      set(val){
         this.arr.forEach(ele => ele.c=val)
      },
      get(){
      // 只要有一个为false就为false
      return this.arr.every(item => item.c)
      }
    }
  },
  methods: {
    btnFn(){
      // 获取状态赋值取反
      return this.arr.forEach(item => item.c=!item.c)
    }
  }
};
</script>